package com.ellricky.baselib.ui.web;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ellricky.baselib.R;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

import static com.ellricky.baselib.ui.web.Constant.JS_DEFAULT_NAME;

import me.jessyan.autosize.internal.CancelAdapt;


/**
 * 4.4 新增
 * 新增原因：
 * 选择需求（H5）复用 {com.qk365.housekeeper.activity.WebViewActivity}时数据出不来，
 * 因此独立重新封装 WebView 页
 */
public class WebViewActivity extends AppCompatActivity implements OnPageListener, CancelAdapt {

    private ProgressBar pBar;
    private WebView webView;

    private String mTitle;

    private Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentRes());
        initUIView();
    }


    protected int getContentRes() {
        return R.layout.base_web_activity_web;
    }

    protected void initUIView() {
        pBar = findViewById(R.id.pb_web);
        webView = findViewById(R.id.web_view);

        WebUtils.initWebviewSettings(webView);
        String jsName = getIntent().getStringExtra(Constant.KEY_JS_NAME);
        if (WebUtils.isEmpty(jsName)) jsName = JS_DEFAULT_NAME;

        webView.addJavascriptInterface(new AppJavaScriptInterface(this, mHandler), jsName);
        webView.requestFocus(View.FOCUS_DOWN);

        webView.setWebChromeClient(new MyWebChromeClient(pBar, this));
//        webView.getSettings().setUseWideViewPort(true);
//        webView.getSettings().setLoadWithOverviewMode(true);
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(true);


        MyWebViewClient webViewClient = new MyWebViewClient(this);
        webView.setWebViewClient(webViewClient);
        // 开启Cookie
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        } else {
            CookieManager.getInstance().setAcceptCookie(true);
        }

        initData();
    }


    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            // 标题栏处理
            mTitle = intent.getStringExtra(Constant.KEY_TITLE);
            setTitle(mTitle);

            // 网页地址
            String url = getIntent().getStringExtra(Constant.KEY_URL);
            Log.i("ying", "开始执行URL加载");
            webView.loadUrl(url);
            Log.i("ying", "开始执行URL加载完成");
        }
    }

    protected void setTitle(String title) {
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backPage();
        }
        return false;
    }


    protected void backPage() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        setResult(RESULT_OK);
        finish();
    }


    @Override
    public void setErrorPage(boolean isShow) {

    }
}
